﻿using CloudShot.Core.Interfaces;

namespace CloudShot.Services
{
  internal class ConfigurationSection : IConfigurationSection
  {
    private readonly string _name;
    private readonly IConfiguration _configuration;

    public ConfigurationSection(string name, IConfiguration configuration)
    {
      _name = name;
      _configuration = configuration;
    }

    public T GetAs<T>()
    {
      T result = _configuration.GetSectionValue<T>(_name);
      return result;
    }

    public T GetOrCreateAs<T>() where T : new()
    {
      T result = GetAs<T>();

      if (Equals(result, default(T)))
        result = new T();

      return result;
    }

    public void SaveAs<T>(T settings)
    {
      _configuration.WriteSectionValue(_name, settings);
    }
  }
}